import type { MesWorkshop, MesWorkshopMachine, MesWorkshopProduct, MesWorkshopUser } from "@/pages/mes/workshop/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
  //类型
  type?: string;
  //编码
  code?: string;
  //名称
  name?: string;
  //当前页码
  page: number;
  //每页数量
  pageSize: number;
}

export interface PageListRes {
  //总记录数
  total: number;
  //数据列表
  records: MesWorkshop[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
  apiClient.post({
    url: "/mes/workshop/pageList",
    data
});

const treeList = (data: PageListReq): Promise<MesWorkshop[]> =>
   apiClient.post({ url: "/mes/workshop/treeList", data });

const getInfo = (id: React.Key): Promise<MesWorkshop> =>
  apiClient.get({
    url: `/mes/workshop/${id}`
});

const add = (data: MesWorkshop): Promise<MesWorkshop> =>
  apiClient.post({
    url: "/mes/workshop",
    data
});

const update = (data: MesWorkshop): Promise<MesWorkshop> =>
  apiClient.put({
    url: "/mes/workshop",
    data
});

const remove = (ids: React.Key[]): Promise<void> =>
  apiClient.delete({
    url: "/mes/workshop",
    params: {
    ids: ids.join(",")
  }
});
const getMachines = (id: React.Key): Promise<MesWorkshopMachine[]> =>
  apiClient.post({
    url: "/mes/workshop/machine/list",
    data: {
      workshopId: id
    }
  });

const getProducts = (id: React.Key): Promise<MesWorkshopProduct[]> =>
  apiClient.post({
    url: "/mes/workshop/product/list",
    data: {
      workshopId: id
    }
  });

const getUsers = (id: React.Key): Promise<MesWorkshopUser[]> =>
  apiClient.post({
    url: "/mes/workshop/user/list",
    data: {
      workshopId: id
    }
  });

// 获取车间下来列表
const getWorkshopSelectList = (): Promise<MesWorkshop[]> => 
  apiClient.get({
    url: "/mes/workshop/selectList"
  })  

// 获取产线下来列表
const getLineSelectList = (workshopId: number): Promise<MesWorkshop[]> => 
  apiClient.get({
    url: `/mes/workshop/line/selectList/${workshopId}`
  })

export const mesWorkshopService = {
  pageList,
  treeList,
  getInfo,
  add,
  update,
  remove,
  getmachines: getMachines,
  getproducts: getProducts,
  getusers: getUsers,
  getWorkshopSelectList,
  getLineSelectList
};

export default mesWorkshopService;